package com.boomsecret.juc.atomic;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @author wangjinliang on 2019/1/17.
 */
public class AtomicIntegerFieldUpdateDemo {
    public static void main(String[] args) {
        AtomicIntegerFieldUpdater<LongUser>  atomicIntegerFieldUpdate = AtomicIntegerFieldUpdater.newUpdater(LongUser.class, "count");
        LongUser user = new LongUser();

        System.out.println(atomicIntegerFieldUpdate.compareAndSet(user, 0, 1));
        System.out.println(user.count);

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.compareAndSet(user, 0, 1));
        System.out.println(user.count);

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.weakCompareAndSet(user, 1, 10));
        System.out.println(user.count);

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.getAndSet(user, 30));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.getAndIncrement(user));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.getAndDecrement(user));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.incrementAndGet(user));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.decrementAndGet(user));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.addAndGet(user, 70));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.getAndUpdate(user, operand -> operand + 100));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.updateAndGet(user, operand -> operand + 100));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.getAndAccumulate(user, 100, ((left, right) -> left + right)));
        System.out.println(atomicIntegerFieldUpdate.get(user));

        System.out.println("-------------");
        System.out.println(atomicIntegerFieldUpdate.accumulateAndGet(user, 100, ((left, right) -> left + right)));
        System.out.println(atomicIntegerFieldUpdate.get(user));
    }

    public static class LongUser {
        volatile int count;
    }
}
